Home:ALL Converter>How to avoid ' in the sql query?

How to avoid ' in the sql query?

Ask Time:2020-10-30T12:47:36         Author:Pi Network Crytocurrency

Json Formatter

I am using MYSQL 5.5. May I know how to avoid ' in the SQL query? Below is my example query:

<?php
$mySelect1 = "'Ahli Majlis'";
$bracket_mySelect1 = "($mySelect1)";
SQL = "SELECT * FROM user where 1 and nama_gelaran in ".$bracket_mySelect1."";
?>

The wrong result I have checked in the console log data is SELECT * FROM user where 1 and nama_gelaran in (&#39;Ahli Majlis&#39;)

Actually I want the result is SELECT * FROM user where 1 and nama_gelaran in ('Ahli Majlis')

What I tried, but it doesn't work:

SQL = SELECT * FROM user where 1 and nama_gelaran in ".html_entity_decode(htmlentities($bracket_mySelect1,ENT_QUOTES),ENT_QUOTES); .";

Author:Pi Network Crytocurrency,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/64602876/how-to-avoid-39-in-the-sql-query
Rattler :

I am unsure how you are executing the queries but it is best practice to use PDO rather then the mysql_ functions. This also improves security and protects against mysql injection.\nBelow is a sample of how to connect and run a PDO query. I have changed your IN to be = as it looks like you are only passing one value.\n$dsn = "mysql:host=localhost;dbname=mydb";\n$user = "dbUsername";\n$passwd = "dbPassword";\n\n$name = 'Ahli Majlis';\n\n$pdo = new PDO($dsn, $user, $passwd);\n$stm = $pdo->query("SELECT * FROM `user` WHERE nama_gelaran = :name");\n$stm->bindParam('name', $name);\n\n$user = $stm->fetch();\n\nprint_r($user);\n",
2020-10-30T12:22:57
yy